package com.redis.test;

import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

import com.redis.api.DataHandler;
import com.redis.bean.KeyValue;
import com.redis.spi.JedisDataHandler;
import com.redis.spi.JedisDataReader;
import com.redis.spi.JedisDataWriter;

public class RedisJunitTestCase {

	private ShardedJedisPool readOnlyPool;
	
	private ShardedJedisPool writeOnlyPool;
	
	private DataHandler handler;
	
	@Before
	public void setUp() throws Exception {
		JedisPoolConfig config = new JedisPoolConfig();
		config.maxActive = 10;
		config.minIdle = 5;
		config.testOnBorrow = true;
		config.maxWait = 60000;
		config.testWhileIdle = true;
		config.whenExhaustedAction = 2;
		config.timeBetweenEvictionRunsMillis = 3600000;
		config.numTestsPerEvictionRun = 5;
		List<JedisShardInfo> shards1 = new ArrayList<JedisShardInfo>();
		JedisShardInfo master = new JedisShardInfo("192.168.16.130", 6379,
				60000);
		master.setPassword("hadoop");
		shards1.add(master);
		List<JedisShardInfo> shards2 = new ArrayList<JedisShardInfo>();
		this.writeOnlyPool = new ShardedJedisPool(config, shards1);
		JedisShardInfo slave = new JedisShardInfo("192.168.16.131", 6379,
				60000);
		slave.setPassword("hadoop");
		shards2.add(slave);
		this.readOnlyPool = new ShardedJedisPool(config, shards2);
		JedisDataHandler dataHandler = new JedisDataHandler();
		dataHandler.setReader(new JedisDataReader(readOnlyPool));
		dataHandler.setWriter(new JedisDataWriter(writeOnlyPool));
		this.handler = dataHandler;
	}
	
	@Test
	public void testGetByteArray() {
		byte[] result = handler.get("cdc-uecp-1000".getBytes());
		System.out.println("get1: " + new String(result));
	}

	@Test
	public void testGetString() {
		String result = handler.get("cdc-uecp-1000");
		System.out.println("get2: " + result);
	}

	@Test
	public void testGetByteArrayByteArray() {
		byte[] values = handler.get("cdc-uecp-1000".getBytes(),null);
		System.out.println("get3: " + new String(values));
		
	}

	@Test
	public void testGetStringString() {
		String result = handler.get("cdc-uecp-1000","ttt");
		System.out.println("get4: " + result);
	}

	@Test
	public void testGetBeanString() {
		User user = handler.getBean("cdc-user-1000");
		System.out.println(user.getId());
		System.out.println(user.getName());
		System.out.println(user.getPhone());
	}

	@Test
	public void testGetBeanStringT() {
		User user = handler.getBean("cdc-user-1000",null);
		System.out.println(user.getId());
		System.out.println(user.getName());
		System.out.println(user.getPhone());
	}

	@Test
	public void testHgetStringString() {
		String key = "CDC-STR-IDX";
		String field = "yangjuanying";
		String value = handler.hget(key, field);
		System.out.println("hget::" + value);
	}

	@Test
	public void testHgetStringStringByteArray() {
		String key = "CDC-STR-IDX";
		String field = "yangjuanying";
	    byte[] value = handler.hget(key.getBytes(), field.getBytes());
		System.out.println("hget::" + new String(value));
	}

	@Test
	public void testHgetBeanStringString() {
		String key = "UECP-USER-INFO";
		String field = "15902025300";
		User user = handler.hgetBean(key, field);
		System.out.println(user.getId());
		System.out.println(user.getName());
		System.out.println(user.getPhone());
	}

	@Test
	public void testHgetBeanStringStringT() {
		String key = "UECP-USER-INFO";
		String field = "15902025301";
		User user = new User();
		user.setId(1002);
		user.setName("黄药师");
		user.setPhone(13800138002l);
		User ret = handler.hgetBean(key, field, user);
		System.out.println(ret.getId());
		System.out.println(ret.getName());
		System.out.println(ret.getPhone());
	}

	@Test
	public void testHgetAll() {
		List<KeyValue> kvs = handler.hgetAll("UECP-USER-INFO");
		System.out.println("all::"+kvs.size());
		User ret = kvs.get(0).getObject();
		System.out.println(ret.getId());
		System.out.println(ret.getName());
		System.out.println(ret.getPhone());
	}

	@Test
	public void testKeys() {
		String[] keys = handler.keys("KEY*");
		for(String key : keys){
			System.out.println(key);
		}
	}

	@Test
	public void testHkeys() {
		String[] keys = handler.hkeys("UECP-USER-INFO");
		for(String key : keys){
			System.out.println(key);
		}
	}

	@Test
	public void testValues() {
		fail("Not yet implemented");
	}

	@Test
	public void testTtl() {
		long ttl = handler.ttl("cdc-user-1008");
		System.out.println("ttl is :" + ttl);
	}

	@Test
	public void testPttl() {
		long pttl = handler.pttl("cdc-uecp-1001");
		System.out.println("pttl is :" + pttl);
	}

	@Test
	public void testType() {
		String type = handler.type("CDC-STR-IDX");
		System.out.println("data type is :" + type);
	}

	@Test
	public void testExists() {
		boolean flag = handler.exists("CDC-STR-IDX");
		System.out.println("data exists:" + flag);
	}

	@Test
	public void testGetRange() {
		fail("Not yet implemented");
	}

	@Test
	public void testHlen() {
		fail("Not yet implemented");
	}

	@Test
	public void testMgetStringArray() {
		fail("Not yet implemented");
	} 

	@Test
	public void testMgetListOfString() {
		fail("Not yet implemented");
	}

	@Test
	public void testHexists() {
		fail("Not yet implemented");
	}

	@Test
	public void testLindex() {
		fail("Not yet implemented");
	}

	@Test
	public void testLrangeByteArrayLongLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testLrangeStringLongLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testLrangeBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testLlen() {
		fail("Not yet implemented");
	}

	@Test
	public void testScard() {
		fail("Not yet implemented");
	}

	@Test
	public void testSismemberStringByteArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSismemberStringString() {
		fail("Not yet implemented");
	}

	@Test
	public void testSismemberStringT() {
		fail("Not yet implemented");
	}

	@Test
	public void testSmembersByteArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSmembersString() {
		fail("Not yet implemented");
	}

	@Test
	public void testSmembersBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testSunionByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSunionStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSunionBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testSdiffByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSdiffStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSdiffBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testSinterByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSinterStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSinterBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZcard() {
		fail("Not yet implemented");
	}

	@Test
	public void testZcount() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrangeByteArrayLongLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrangeStringLongLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrangeBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrangeByScoreByteArrayDoubleDouble() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrangeByScoreStringDoubleDouble() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrangeByScoreBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrankStringByteArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrankStringString() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrankBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrangeByteArrayLongLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrangeStringLongLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrangeBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZscoreStringByteArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testZscoreStringString() {
		fail("Not yet implemented");
	}

	@Test
	public void testZscoreBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrangeByScoreByteArrayDoubleDouble() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrangeByScoreStringDoubleDouble() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrangeByScoreBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrankStringByteArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrankStringString() {
		fail("Not yet implemented");
	}

	@Test
	public void testZrevrankBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZgetPage() {
		fail("Not yet implemented");
	}

	@Test
	public void testSetByteArrayByteArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSetnxByteArrayByteArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSetnxStringString() {
		fail("Not yet implemented");
	}

	@Test
	public void testSetnxStringStringInt() {
		fail("Not yet implemented");
	}

	@Test
	public void testSetByteArrayByteArrayInt() {
		fail("Not yet implemented");
	}

	@Test
	public void testSetStringString() {
		boolean flag = handler.set("cdc-uecp-1000", "那些年我们一起追的女孩子");
		System.out.println("set3::" + flag);
	}

	@Test
	public void testSetStringStringInt() {
		boolean flag = handler.set("cdc-uecp-1001", "那些年我们一起追的女孩子11", 600);
		System.out.println("set4::" + flag);
	}

	@Test
	public void testSetBeanStringT() {
		User user = new User();
		user.setId(1000);
		user.setName("欧阳锋");
		user.setPhone(13800138000l);
		boolean flag = handler.setBean("cdc-user-1002", user);
		System.out.println("set4::" + flag);
	}

	@Test
	public void testSetBeanStringTInt() {
		User user = new User();
		user.setId(1003);
		user.setName("欧阳克");
		user.setPhone(13800138002l);
		boolean flag = handler.setBean("cdc-user-1002", user, 8000);
		System.out.println("set5::" + flag);
	}

	@Test
	public void testGetSetByteArrayByteArray() {
		byte[] values = handler.getSet("cdc-user-1003".getBytes(), "嘎嘎嘎3".getBytes());
		System.out.println("values1::" + new String(values));
	}

	@Test
	public void testGetSetByteArrayByteArrayInt() {
		byte[] values = handler.getSet("cdc-user-1004".getBytes(), "嘎嘎嘎5".getBytes(), 9000);
		System.out.println("values2::" + new String(values));
	}

	@Test
	public void testGetSetStringString() {
		String values = handler.getSet("cdc-user-1005", "嘎嘎嘎67");
		System.out.println("values3::" + values);
		
	}

	@Test
	public void testGetSetStringStringInt() {
		String values = handler.getSet("cdc-user-1006", "嘎嘎嘎67",9000);
		System.out.println("values4::" + values);
	}

	@Test
	public void testGetSetBeanStringT() {
		User user = new User();
		user.setId(1004);
		user.setName("黄蓉");
		user.setPhone(13800138003l);
		User ret = handler.getSetBean("cdc-user-1007", user);
		System.out.println("values5::" + ret);
	}

	@Test
	public void testGetSetBeanStringTInt() {
		User user = new User();
		user.setId(1005);
		user.setName("郭靖");
		user.setPhone(13800138004l);
		User ret = handler.getSetBean("cdc-user-1008", user,100000);
		System.out.println("values6::" + ret);
	}

	@Test
	public void testAppendStringByteArray() {
		Long length = handler.append("cdc-user-1009", "abcd".getBytes());
		System.out.println("append length1:::" + length);
	}

	@Test
	public void testAppendStringString() {
		Long length = handler.append("cdc-user-1010", "abcd中国人");
		System.out.println("append length2:::" + length);
	}

	@Test
	public void testMset() {
		int total = 10000;
		String[]  kvs = null;
		List<String> kv = new ArrayList<String>();
		long begin = System.currentTimeMillis();
		for(int i=0;i<total;i++){
			if(i%2 ==0){
				kv.add("KEY--" + i);
			}else{
				kv.add("VALUE--" + i);
			}
			if(kv.size() == 100){
				kvs = new String[100];
				kv.toArray(kvs);
				boolean flag = handler.mset(kvs);
				kv.clear();
				System.out.println("mset::" + flag   +  " cost::"  + (System.currentTimeMillis()-begin));
				System.out.println("current::"  + i);
			}
		}
		long end = System.currentTimeMillis();
		System.out.println(" total cost::"  + (end-begin));
	}

	@Test
	public void testMsetnx() {
	}

	@Test
	public void testDel() {
		boolean flag = handler.del("KEY--4");
		System.out.println("delete:::" + flag);
	}

	@Test
	public void testIncr() {
		long counter = handler.incr("cdc-user-1011");
		System.out.println("counter::" + counter);
	}

	@Test
	public void testIncrBy() {
		long counter = handler.incrBy("cdc-user-1011",90);
		System.out.println("counter2::" + counter);
	}

	@Test
	public void testDecr() {
		long counter = handler.decr("cdc-user-1011");
		System.out.println("counter3::" + counter);
	}

	@Test
	public void testDecrBy() {
		long counter = handler.decrBy("cdc-user-1011",2);
		System.out.println("counter4::" + counter);
	}

	@Test
	public void testIncrByFloat() {
		Double counter = handler.incrByFloat("cdc-user-1012",2.91);
		System.out.println("counter5::" + counter);
	}

	@Test
	public void testHsetStringStringByteArray() {
		boolean flag = handler.hset("cdc-user-1013", "field", "好好的".getBytes());
		System.out.println("hset1:::" + flag);
		
	}

	@Test
	public void testHsetnxStringStringByteArray() {
		
	}

	@Test
	public void testHsetStringStringString() {
		String key = "CQ-2013-12-V4";
		String value = "password00000000000000000fa0f0a0f0e0wr0e0w00000password00000000000000000fa0f0a0f0e0wr0e0w00000";
		value = value + value;
		String chPrefix = "JM_CH_" ;
		System.out.println("total count::" + handler.hlen(key));
//		handler.del(key);
		Map<String, String> hash = new HashMap<String, String>();
		long begin = System.currentTimeMillis();
//		int totalCount = 1000000;
//		for(int i=1;i<=totalCount;i++){
//			hash.put(chPrefix + i, value + "--" + i);
//			if(i%100000 == 0){
//				handler.hmset(key, hash);
//				System.out.println("crruent::"  + i);
//				hash.clear();
//			}
//		}
//		if(!hash.isEmpty()){
//			handler.hmset(key, hash);
//		}
		
		List<KeyValue> values = handler.hgetAll(key);
		long end = System.currentTimeMillis();
//		System.out.println("total count::" + handler.hlen(key));
		System.out.println("read data size::" + values.size());
//		System.out.println("value:" + handler.hget(key, "JM_CH_"+ totalCount));
		System.out.println("execute cost::" + (end-begin) + " ms");
	}

	@Test
	public void testHsetnxStringStringString() {
		String key = "CDC-STR-IDX";
		String field = "yangjuanying";
		String value = "password1111";
		boolean flag = handler.hsetnx(key, field, value);
		System.out.println("hsetnx::" + flag);
	}

	@Test
	public void testHsetBean() {
		String key = "UECP-USER-INFO";
		String field = "15902025300";
		User user = new User();
		user.setId(1001);
		user.setName("洪七公");
		user.setPhone(13800138001l);
		boolean flag = handler.hsetBean(key, field, user);
		System.out.println("hsetBean::" + flag);
	}

	@Test
	public void testHsetnxBean() {
		
	}

	@Test
	public void testHdel() {
		fail("Not yet implemented");
	}

	@Test
	public void testHincrBy() {
		fail("Not yet implemented");
	}

	@Test
	public void testSremStringByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testLpushStringByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testLpushStringStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testLpushStringTArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testLpushxStringByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testLpushxStringStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testLpushxStringTArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpushStringByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpushStringStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpushStringTArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpushxStringByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpushxStringStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpushxStringTArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSremStringStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSaddStringByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testSaddStringStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testLpop() {
		fail("Not yet implemented");
	}

	@Test
	public void testLsetStringByteArrayLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testLsetStringStringLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testLsetBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testLremStringByteArrayLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testLremStringStringLong() {
		fail("Not yet implemented");
	}

	@Test
	public void testLpopBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpop() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpopBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpoplpush() {
		fail("Not yet implemented");
	}

	@Test
	public void testRpoplpushBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testLtrim() {
		fail("Not yet implemented");
	}

	@Test
	public void testZaddByteArrayMapOfDoublebyte() {
		fail("Not yet implemented");
	}

	@Test
	public void testZaddStringMapOfDoubleString() {
		fail("Not yet implemented");
	}

	@Test
	public void testZaddBean() {
		fail("Not yet implemented");
	}

	@Test
	public void testZaddPage() {
		fail("Not yet implemented");
	}

	@Test
	public void testZincrByStringDoubleByteArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testZincrByStringDoubleString() {
		fail("Not yet implemented");
	}

	@Test
	public void testZincrByStringDoubleT() {
		fail("Not yet implemented");
	}

	@Test
	public void testZremStringByteArrayArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testZremStringStringArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testZremStringTArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testZremrangeByRank() {
		fail("Not yet implemented");
	}

	@Test
	public void testZremrangeByScore() {
		fail("Not yet implemented");
	}

}
